<template>
  <div class="home">
    <div class="title-container">
      <div class="search-content">
        <el-form :model="searchModel" :inline="true">
          <el-form-item>
            <el-input
              v-model="searchModel.zoneName"
              placeholder="请输入区域名称"
              prefix-icon="el-icon-search"
            />
          </el-form-item>
        </el-form>
        <div class="search-btn">
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="searchClick"
            size="medium"
          >
            搜索</el-button
          >
        </div>
      </div>
    </div>

    <el-card>
      <div class="addAccount">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="medium"
          @click="addClick"
        >
          添加区域</el-button
        >
        <el-button
          type="success"
          icon="el-icon-refresh"
          size="medium"
          @click="refresh"
          >刷新</el-button
        >
      </div>
      <el-table
        v-loading="loading"
        :data="tableData"
        style="width: 100%"
        class="elTable"
        ref="tableRef"
        border
        row-key="id"
        :tree-props="{ children: 'bZoneList' }"
      >
        <el-table-column prop="zoneName" label="区域名称" align="center" />
        <el-table-column prop="zoneLevel" label="级别" align="center" />
        <el-table-column prop="remake" label="备注" align="center" />
        <el-table-column label="操作" width="400" align="center">
          <template #default="{ row }">
            <el-button type="primary" size="mini" @click="addRow(row)"
              >添加子区域
            </el-button>

            <el-button type="primary" size="mini" @click="handlerEdit(row)"
              >编辑
            </el-button>

            <el-button type="danger" size="mini" @click="handlerDelete(row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- <pagination
        class="pagination"
        :total="count"
        :page.sync="pageIndex"
        :limit.sync="pageSize"
        @pagination="pagination"
      /> -->
    </el-card>
    <!-- 弹框 -->

    <yc-dialog ref="ycDialog" :centerTitle="centerTitle">
      <template #content>
        <div class="dialog-content">
          <el-form ref="form" :rules="rules" :model="form" label-width="90px">
            <el-form-item label="区域名称" prop="zoneName">
              <el-input v-model="form.zoneName" />
            </el-form-item>
            <el-form-item label="备注" prop="zonelevel">
              <el-input v-model="form.remake" />
            </el-form-item>
          </el-form>
        </div>
      </template>
      <template #footer>
        <el-button type="primary" @click="dialogSave">确定</el-button>
        <el-button @click="$refs.ycDialog.dialogVisible = false"
          >取消
        </el-button>
      </template>
    </yc-dialog>

    <!-- 封装操作后弹框 -->
    <global-message
      v-if="isGlobalMessageShow"
      @receive="receive"
      :message="message"
    />
  </div>
</template>

<script>
import {
  selectLevelZone,
  deleteZonById,
  updateZon,
  addZon,
  selectZonePage
} from "@/api/regionManage"
import ycDialog from "@/components/pageDialog"
export default {
  data() {
    return {
      form: {},
      isGlobalMessageShow: false,
      message: "",
      tableData: [],
      count: 0,
      pageIndex: 1,
      pageSize: 20,
      loading: false,
      centerTitle: "",
      searchModel: {},
      zoneFathercode: "",
      rules: {
        zoneName: [{ required: true, message: "请输入账号", trigger: "blur" }],
        zoneLevel: [{ required: true, message: "请选择角色", trigger: "blur" }]
      }
    }
  },
  components: {
    ycDialog
  },
  created() {
    this.getData()
  },
  methods: {
    async getData() {
      const data = await selectLevelZone()
      this.tableData = data.object
    },
    pagination(pageItem) {
      this.getData(pageItem.page, pageItem.limit)
    },
    addRow(row) {
      this.centerTitle = "添加子区域"
      this.$refs.ycDialog.dialogVisible = true
      this.form = {}
      this.$refs.form?.clearValidate()
      this.zoneFathercode = row.zoneCode
      console.log(row, "row")
    },
    addClick() {
      this.zoneFathercode = ""
      this.centerTitle = "添加区域"
      this.$refs.ycDialog.dialogVisible = true
      this.form = {}
      this.$refs.form?.clearValidate()
    },
    handlerEdit(row) {
      this.centerTitle = "编辑"
      this.$refs.ycDialog.dialogVisible = true
      this.$refs.form?.clearValidate()
      this.form = { ...row }
    },
    async handlerDelete({ id }) {
      this.$confirm("确定删除此用户吗", "删除操作", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "error",
        center: true
      }).then(async () => {
        const { message } = await deleteZonById({ id })
        if (message == "删除成功") {
          this.getData()
          this.isGlobalMessageShow = true
          this.message = "success"
        } else {
          this.message = "error"
        }
      })
    },
    async selectZonePage() {
      const data = await selectZonePage({
        pageIndex: 1,
        pageSize: 100,
        zoneName: this.searchModel.zoneName
      })
      this.tableData = data.object.data
    },
    // 搜索按钮
    async searchClick() {
      this.selectZonePage()
    },
    dialogSave() {
      this.$refs.form.validate(async (v) => {
        if (v) {
          if (this.form.id) {
            const { zoneName, id } = this.form
            const { message, status } = await updateZon({
              zoneName,
              id
            })
            this.handleStatus(status, message)
          } else {
            const { message, status } = await addZon({
              ...this.form,
              zoneFathercode: this.zoneFathercode
            })
            this.handleStatus(status, message)
          }
        }
      })
    },
    handleStatus(status, message) {
      console.log(message, "3333")
      this.isGlobalMessageShow = true
      if (status == 200) {
        this.refresh()
        this.$refs.ycDialog.dialogVisible = false
        this.message = "success"
      } else {
        this.message = "error"
      }
    },
    refresh() {
      this.searchModel = {}
      this.getData()
    },
    receive() {
      this.isGlobalMessageShow = false
    }
  }
}
</script>

<style lang="scss" scoped>
.status {
  display: inline-block;
  padding: 0 10px;
}
.title-container {
  display: flex;
  justify-content: flex-end;
  height: 60px;
  .search-content {
    margin-left: 20px;
    display: flex;
  }
  .addAccount {
    margin: 10px 20px;
  }
}
.pagination {
  display: flex;
  justify-content: flex-end;
}
.content {
  height: 600px;

  .drawer-title {
    font-weight: 400;
    font-size: 24px;
    color: #000000;
    text-align: center;
    padding: 36px 0;
  }
  .form {
    padding: 0 10px;
  }
}
.footer {
  height: 360px;
  display: flex;
  align-items: flex-end;
  // justify-content: st-end;
  padding: 5px 20px;
}
</style>
